using System.Collections.Generic;
using System.IO;
using System.Linq;
using System;

namespace SolutionOrchestrator
{
	public class SolutionSettingsProviderImpl : ISolutionSettingsProvider
	{
		readonly IMySettingsProvider _settingsProvider;

		public SolutionSettingsProviderImpl(IMySettingsProvider settingsProvider)
		{
			_settingsProvider = settingsProvider;
		}

		public string SolutionPath
		{
			get
			{
				return _settingsProvider.Get.CurrentRootPath;
			}
		}

		SolutionSettings _last;
		string _lastPath;

		public SolutionSettings Get
		{
			get
			{
				if (_lastPath != SolutionPath)
				{
					string xaml = null;
					if (PathHelper.FileExists(FileName))
					{
						xaml = File.ReadAllText(FileName);
					}
					var settings = XamlHelper.ParseXaml<SolutionSettings>(xaml);
					settings.Path = FileName;
					_last = settings;
					_lastPath = SolutionPath;
				}
				return _last;
			}
		}

		string FileName
		{
			get
			{
				return Path.Combine(SolutionPath, "slnorch.config");
			}
		}

	}
}